package blog.gui;

import java.util.Calendar;
import java.util.Date;

import javax.microedition.lcdui.*;
import blog.*;
import blog.controller.*;
import blog.db.*;

public class BlogEntryListGui extends List implements IBaseGui, CommandListener {
	
	private Command cmdBack = new Command( ConstString.CMD_BACK, Command.BACK, 0 );
	private Command cmdRemove = new Command( ConstString.CMD_REMOVE, Command.SCREEN, 3);
	private Command cmdEdit = new Command( ConstString.CMD_EDIT, Command.SCREEN, 2 );
	private Command cmdPost = new Command( ConstString.CMD_POST, Command.SCREEN, 1 );
	private Command cmdShow = new Command( ConstString.CMD_SHOW, Command.SCREEN, 0 );
	private Command cmdConfigMaxLen = new Command( ConstString.CMD_CONFIG, Command.SCREEN, 4);
	
	private BlogEntry[] data;
	
	private boolean searchCondition_publishOk;

	private static BlogEntryListGui _instance;
	public synchronized static BlogEntryListGui getInstance()
	{
		if ( _instance==null )
		{
			_instance = new BlogEntryListGui();
		}
		return _instance;
	}
	
	public BlogEntryListGui() {
		super(ConstString.GUI_TITLE_BLOG_ENTRY_LIST, List.IMPLICIT);
		
		searchCondition_publishOk = false;
		
		addCommand(cmdBack);
		addCommand(cmdEdit);
		addCommand(cmdRemove);
		addCommand(cmdPost);
		addCommand(cmdConfigMaxLen);
		setCommandListener(this);
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_BLOG_ENTRY_LIST;
	}

	public void notifyChanged(Object param) {

	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==cmdBack )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmdRemove )
		{
			BlogEntryDB.delete(data[this.getSelectedIndex()]);
			this.reloadData();
		}
		else if ( cmd==cmdEdit )
		{
			BlogEntryGui nextScreen = BlogEntryGui.getInstance();
			nextScreen.setOpType(BlogEntryGui.OP_TYPE_EDIT, BlogEntryDB.getById(data[this.getSelectedIndex()].getId(), data[this.getSelectedIndex()].getIndexId()));
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmdPost )
		{
			SysRuntime.actionManager.addAction(this, ConstAction.ACT_POST_BLOG, BlogEntryDB.getById(data[this.getSelectedIndex()].getId(), data[this.getSelectedIndex()].getIndexId()));
		}
		else if ( cmd==List.SELECT_COMMAND )
		{
			BlogEntryGui nextScreen = BlogEntryGui.getInstance();
			if ( this.searchCondition_publishOk )
			{
				nextScreen.setOpType(BlogEntryGui.OP_TYPE_SHOW, BlogEntryDB.getById(data[this.getSelectedIndex()].getId(), data[this.getSelectedIndex()].getIndexId()));
			}
			else
			{
				nextScreen.setOpType(BlogEntryGui.OP_TYPE_EDIT, BlogEntryDB.getById(data[this.getSelectedIndex()].getId(), data[this.getSelectedIndex()].getIndexId()));
			}
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmdShow )
		{
			BlogEntryGui nextScreen = BlogEntryGui.getInstance();
			nextScreen.setOpType(BlogEntryGui.OP_TYPE_SHOW, BlogEntryDB.getById(data[this.getSelectedIndex()].getId(), data[this.getSelectedIndex()].getIndexId()));
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==cmdConfigMaxLen )
		{
			ConfigGui nextScreen = new ConfigGui( this.searchCondition_publishOk );
			SysRuntime.guiManager.show(nextScreen);
		}
	}
	
	public void reloadData()
	{
		data = null;
		data = BlogEntryDB.getIndex(SysRuntime.curAccount.getId(), searchCondition_publishOk);
		
		this.deleteAll();
		removeCommand(cmdEdit);
		removeCommand(this.cmdPost);
		removeCommand(this.cmdRemove);
		removeCommand(this.cmdShow);
		
		int maxSize = searchCondition_publishOk?SysRuntime.curAccount.getLenHistoryList():SysRuntime.curAccount.getLenEntryList();
		if ( data!=null && data.length>0 )
		{
			Calendar c = Calendar.getInstance();
			StringBuffer sb = new StringBuffer();
			for( int i=0; i<Math.min(data.length, maxSize); i++ )
			{
				c.setTime(new Date(data[i].getCreatedDate()));
				sb.delete(0, sb.length());
				sb.append(data[i].getTitle()).append(" (").append(c.get(Calendar.MONTH)+1).append("/").append(c.get(Calendar.DAY_OF_MONTH)).append("/").append(c.get(Calendar.YEAR)).append( ")" );
				if ( searchCondition_publishOk )
				{
					this.append(sb.toString(), BlogMainViewGui.getInstance().historyImg );
				}
				else
				{
					append(sb.toString(), BlogMainViewGui.getInstance().draftImg );
				}
			}
			if ( !searchCondition_publishOk )
			{
				addCommand(cmdEdit);
				addCommand(cmdPost);
				addCommand(cmdRemove);
			}
			else 
			{
				addCommand( cmdShow );
				addCommand(cmdRemove);
			}
		}
		else
		{
			this.append(ConstString.GUI_BLOG_ENTRY_LIST_NO_DATA, null);
		}
	}
	
	public void setCondition( boolean publishOk )
	{
		this.searchCondition_publishOk = publishOk;
	}

}
